package cn.com.cfae.iras.doc.analyze.parse;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;

public class GsonUtil {


    private static GsonUtil instance = new GsonUtil();


    private GsonUtil() {
    }

    public static GsonUtil getInstance() {
        return instance;
    }

    /**
     * @param jsonString
     * @return
     */
    public JsonElement parse(String jsonString) {
        JsonParser jsonParser = new JsonParser();
        return jsonParser.parse(jsonString);
    }

    public String toJson(Object object){
        Gson gson = new GsonBuilder().create();
        return gson.toJson(object);
    }




    /**
     * @param reader
     * @return
     */
    public JsonElement parse(Reader reader) {
        JsonParser jsonParser = new JsonParser();
        return jsonParser.parse(reader);
    }


    public JsonElement parse(File file) throws FileNotFoundException {
        return parse(new FileReader(file));
    }


}
